package cn.edu.wfit.service;

import cn.edu.wfit.entity.Users;
import cn.edu.wfit.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class AuthorizeService implements UserDetailsService {
    @Autowired
    private UsersMapper usersMapper;
    @Override
    public LoginUser loadUserByUsername(String username) throws UsernameNotFoundException {
        Users account = usersMapper.findUsersByName(username);

        if(Objects.isNull(account)){
            throw new UsernameNotFoundException("没有该用户");
        }
       // return User.withUsername(account.getUsername()).password(account.getPassword()).roles("admin").build();
        String[] authority= {"ROLE_admin"};
        return new LoginUser(account,authority);
    }
}
